package cn.sq.musicserver.plug.model.neteasecloudmusic.service;

import cn.sq.musicserver.exception.PlugError;
import cn.sq.musicserver.plug.model.neteasecloudmusic.constant.NeteaseCloudMusicConstant;
import cn.sq.musicserver.plug.model.neteasecloudmusic.entity.*;
import com.google.gson.Gson;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: SQ
 * Date: 2020/12/11
 * Time: 10:39
 * Description:处理请求
 */
//@Service
@Component
public class NeteaseCloudMusicService {
    /**
     * 登录
     * @param loginParam
     * @return 登录是否成功
     */
    public Boolean NeteaseCloudMusicLogin(LoginParam loginParam) throws IOException {
        Connection connect = Jsoup.connect(NeteaseCloudMusicConstant.EMAIL_LOGIN);
        connect.headers(NeteaseCloudMusicConstant.HEADERS);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        connect.data("email",loginParam.getEmail());
        connect.data("password",loginParam.getPassword());
        Document post = connect.post();
        String text = post.body().text();
        NeteaseCloudMusicConstant.COOKIES = connect.response().cookies();
        System.out.println(text);
        LoginResult loginResult = new Gson().fromJson(post.body().text(), LoginResult.class);
        if (loginResult.getCode()==200&&loginResult.getToken()!=null&&loginResult.getToken().length()>0){
            return true;
        }
        return false;
    }

    /**
     * 检查是否已经登录
     * @return
     * @throws IOException
     */
    public Boolean neteaseCloudMusiCloginStatus() throws IOException {
        if (NeteaseCloudMusicConstant.COOKIES==null){
            return false;
        }
        Connection connect = Jsoup.connect(NeteaseCloudMusicConstant.LOGIN_STATUS).cookies(NeteaseCloudMusicConstant.COOKIES);
        connect.headers(NeteaseCloudMusicConstant.HEADERS);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        Document post = connect.get();
        LoginStatusResult loginStatusResult = new Gson().fromJson(post.body().text(), LoginStatusResult.class);
        if (loginStatusResult.getCode()==200&&loginStatusResult.getProfile().getUserId()>0){
            return true;
        }
        return false;
    }

    /**
     * 搜索歌曲信息
     * @param searchParam 搜索参数
     * @return 歌曲搜索结果
     * @throws IOException
     */
    public SearchResult NeteaseCloudMusiCloudsearch(SearchParam searchParam) throws IOException {
        Connection connect = Jsoup.connect(NeteaseCloudMusicConstant.SEARCH);
        connect.headers(NeteaseCloudMusicConstant.HEADERS);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        if(searchParam.getKeywords()!=null){
            connect.data("keywords",searchParam.getKeywords());
        }else {
            throw new PlugError("请输入搜索条件");
        }
        if(searchParam.getLimit()!=null) {
            connect.data("limit", searchParam.getLimit());
        }
        if(searchParam.getType()!=null) {
            connect.data("type", searchParam.getType());
        }
        Document post = connect.post();
        SearchResult searchResult = new Gson().fromJson(post.body().text(), SearchResult.class);
        return searchResult;
    }
    /**
     * 搜索歌曲信息(更全的)
     * @param searchParam 搜索参数
     * @return 歌曲搜索结果
     * @throws IOException
     */
    public CloudSearchResult NeteaseCloudMusiCloudCloudsearch(SearchParam searchParam) throws IOException {
        Connection connect = Jsoup.connect(NeteaseCloudMusicConstant.CLOUD_SEARCH);
        connect.headers(NeteaseCloudMusicConstant.HEADERS);
        connect.ignoreContentType(true);
        connect.ignoreHttpErrors(true);
        if(searchParam.getKeywords()!=null){
            connect.data("keywords",searchParam.getKeywords());
        }else {
            throw new PlugError("请输入搜索条件");
        }
        if(searchParam.getLimit()!=null) {
            connect.data("limit", searchParam.getLimit());
        }
        if(searchParam.getType()!=null) {
            connect.data("type", searchParam.getType());
        }
        Document post = connect.post();
        String text = post.body().text();
        System.out.println(text);
        CloudSearchResult cloudSearchResult = new Gson().fromJson(post.body().text(), CloudSearchResult.class);
        return cloudSearchResult;
    }


}
